import axios from 'axios';
import { baseURL } from '@/config/url.js';
import { notification } from 'ant-design-vue';
import vSvgIcon from '@/components/svgIcon/Index.vue'
const server = axios.create({
  baseURL,
  timeout: 15000,
});
// request拦截器
server.interceptors.request.use((config) => config);
//response 拦截器
server.interceptors.response.use(
  (res) => res.data,
  (err) => {
    console.log('err',err)
    handleErrorResponse(err);
    return Promise.reject(err);
  }
);
const ERROR_MESSAGES = {
  400: '错误请求',
  401: '未授权',
  403: '服务器拒绝访问',
  404: '请求资源不存在',
  405: '请求方法未允许',
  408: '网络请求超时',
  500: '服务器内部错误',
  501: '服务器未实现请求功能',
  502: '错误网关',
  503: '服务不可用',
  504: '网关超时',
  505: 'http版本不支持该请求',
};
const handleErrorResponse = (error) => {
  const status = error?.response?.status;
  const message = ERROR_MESSAGES[status] || '未知错误';
  if (status && ERROR_MESSAGES[status]) {
    console.log(status);
    notification.open({
      message: 'Error',
      description: ERROR_MESSAGES[status],
      icon: () => <vSvgIcon name="closeOne"></vSvgIcon>,
    });
  } else {
    console.error(error);
    notification.open({
      message: 'Error',
      description: message,
      icon: () => <vSvgIcon name="closeOne"></vSvgIcon>,
    });
  }
};
export default server;
